﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Filter;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;        

    /// <summary>Filter specified properties form.</summary>
    public partial class MeanshiftForm : Form, IOptionsForm
    {
        private readonly Meanshift filter;

        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MeanshiftForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public MeanshiftForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess,
                                new Meanshift()) as Meanshift;
        }

                /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            Bitmap b = filter.Apply(Processor.OriginalImage);
            string s = Program.L.Text[40] + Program.L.Text[120] + ": "  +
            Program.L.Text[121] + "=" + filter.MinimumShifted + "; " +
            Program.L.Text[122] + "=" + filter.MaximumIteration + "; " +
            Program.L.Text[123] + "=" + filter.SpatialRadius + "; " +
            Program.L.Text[124] + "=" + filter.ColorDistance + "; ";
            Processor.Change(b, s); 
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[120]; // Name of filter, e.g. Grayscale
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57]; // Options

            lbMinimumShiftedText.Text = Program.L.Text[121];
            lbMaximumIterationText.Text = Program.L.Text[122];
            lbSpatialRadiusText.Text = Program.L.Text[123];
            lbColorDistanceText.Text = Program.L.Text[124];
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }

        private void TrBarMinimumShiftedValueChanged(object sender, EventArgs e)
        {
            filter.MinimumShifted = trBarMinimumShifted.Value;
            lbMinimumShiftedValue.Text = filter.MinimumShifted.ToString(
                                                CultureInfo.InvariantCulture);
        }

        private void TrBarMaximumIterationValueChanged(object sender, EventArgs e)
        {
            filter.MaximumIteration = trBarMaximumIteration.Value;
            lbMaximumIterationValue.Text = filter.MaximumIteration.ToString(
                                                CultureInfo.InvariantCulture);
        }

        private void TrBarSpatialRadiusValueChanged(object sender, EventArgs e)
        {
            filter.SpatialRadius = trBarSpatialRadius.Value;
            lbSpatialRadiusValue.Text = filter.SpatialRadius.ToString(
                                                CultureInfo.InvariantCulture);
        }

        private void TrBarColorDistanceValueChanged(object sender, EventArgs e)
        {
            filter.ColorDistance = trBarColorDistance.Value;
            lbColorDistanceValue.Text = filter.ColorDistance.ToString(
                                                CultureInfo.InvariantCulture);
        }

    }
}
